<?php

namespace GanYiInfo\Utils;

class Str
{
    /**
     * @param $mobile
     * @return false|int
     */
    public static function isMobile($mobile)
    {
        return preg_match("/^1[3|4|5|6|7|8]{1}\d{9}$/", $mobile);
    }

    /**
     * 校验$value是否非空
     *  if not set ,return true;
     *    if is null , return true;
     *
     * @param $value
     * @return bool
     **/
    public static function checkEmpty($value)
    {
        if (!isset($value))
            return true;
        if ($value === null)
            return true;
        if (trim($value) === "")
            return true;

        return false;
    }

    /**
     * @param int $length
     * @return string
     * @throws \Exception
     */
    public static function random($length = 16)
    {
        $string = '';
        while (($len = strlen($string)) < $length) {
            $size = $length - $len;
            $bytes = random_bytes($size);
            $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
        }

        return $string;
    }
}